﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace EzDesk.DevHelper.DbMgr.Models
{
    public class FeedFormModel
    {
        public IList<object> FeedList { get; set; }

    }

    public class KeyValueItem
    {
        public string Text { get; set; }
        public string Value { get; set; }
    }

    static class Helper
    {
        public static string MergeDataToEzML(string originalStr, IDictionary<string, string> parameters)
        {
            Regex regex = new Regex(
                @"(?<={)\w+(?=})",
                RegexOptions.IgnoreCase | RegexOptions.Compiled
                );

            string stringInput = originalStr;
            string result = originalStr;
            MatchCollection coll = regex.Matches(stringInput);


            foreach (Match match in coll)
            {
                string variable = match.Value as string;
                if (parameters.Keys.Contains(variable))
                {
                    result = result.Replace("{" + variable + "}", parameters[variable]);
                }
                else
                {
                    //TODO: 包含没有定义的变量，抛出异常
                    throw new Exception("FeedData与Template合并错误！  " + match.Value + "找不到。");
                }
            }

            return result;
        }
    }
}
